package com.test.servicedemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


/**这是使用后台服务的学习例子*/
public class MainActivity extends AppCompatActivity {
    /**参数设置*/
    Button startServiceButton;// 启动服务按钮
    Button shutDownServiceButton;// 关闭服务按钮
    Button startBindServiceButton;// 启动绑定服务按钮
    Button sendBroadcast;// 使用广播


    CountService countService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("MainStadyServics", "setContentView");
        setContentView(R.layout.activity_main);
        getWidget();
        regiestListener();
    }

    /**获得组件*/
    public void getWidget(){
        startServiceButton = (Button)findViewById(R.id.startServerButton);
        startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);
        shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);
        sendBroadcast = (Button)findViewById(R.id.sendBroadcast);


    }
    /**为按钮添加监听*/
    public void regiestListener(){
        startServiceButton.setOnClickListener(startService);
        shutDownServiceButton.setOnClickListener(shutdownService);
        startBindServiceButton.setOnClickListener(startBinderService);
        sendBroadcast.setOnClickListener(broadcastReceiver);


    }
    /**启动服务的事件监听*/
    public Button.OnClickListener startService = new Button.OnClickListener(){
        public void onClick(View view){
            /**单击按钮时启动服务*/
            Intent intent = new Intent(MainActivity.this,CountService.class);
            startService(intent);
            Log.v("MainStadyServics", "start Service");
        }
    };
    /**关闭服务*/
    public Button.OnClickListener shutdownService = new Button.OnClickListener(){
        public void onClick(View view){
            /**单击按钮时启动服务*/
            Intent intent = new Intent(MainActivity.this,CountService.class);
            /**退出Activity是，停止服务*/
            stopService(intent);
            Log.v("MainStadyServics", "shutDown serveice");
        }
    };
    /**打开绑定服务的Activity*/
    public Button.OnClickListener startBinderService = new Button.OnClickListener(){
        public void onClick(View view){
            /**单击按钮时启动服务*/
            Intent intent = new Intent(MainActivity.this,UseBrider.class);
            startActivity(intent);
            Log.v("MainStadyServics", "start Binder Service");
        }
    };
    /**打开广播学习的按钮*/
    public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
        public void onClick(View view){
            Intent intent = new Intent(MainActivity.this,UseBroadcast.class);
            startActivity(intent);
            Log.v("MainStadyServics","start broadcast");
        }
    };


    /***/
    protected void onDestroy(){
        super.onDestroy();
        Intent intent = new Intent(MainActivity.this,CountService.class);
        /**退出Activity是，停止服务*/
        stopService(intent);
    }
}
